package gameEngine.agents.observationSystem;

import gameEngine.agents.observationSystem.samples.Sample;

import java.util.Collection;

/***
 * @author vladimir && Alon
 *
 *	Type of Observation (implements it)
 *
 *	This class use the ObservationData (that implements the sample) 
 *	and not sample it self!!
 *
 *	arrayOfAgentsObservation - array of AgentsObservation, one AgentsObservation
 *	for every agent in the game , arrayOfAgentsObservation[0] for agent 1, 
 *	arrayOfAgentsObservation[2] for agent 2, and so also
 *
 */
public class SimpleObservation implements Observation{

	private	AgentsObservation agentsObservations[];
	
	//CTOR
	public SimpleObservation(int numAgents)
	{
		// initialize  the observations for every agent
		agentsObservations = new AgentsObservation[numAgents];
		for(int i = 0; i < numAgents; i++){
			//Agents ID start from 1, thats why "+ 1"
			agentsObservations[i]=new AgentsObservation(i + 1);
		}
	}
	
	
	/**
	 *  add new "observation" to the data 
	 * @param sample
	 */
	public void addSample(Sample sample) {
		// TODO Auto-generated method stub
		if (sample == null) {return;}
		
		int id = sample.getID();
			
		//Agents id start from 1, thats why "- 1"
		agentsObservations[id -1].addSample(sample);
	}

	/**
	 * Return the sample from the last round
	 * @return Sample
	 */
	public Sample getLastSample() {
		Sample lastSample = null;
		//go over the agents and find which have the last round
		for (int i = 0; i < agentsObservations.length; ++i)
		{
			Sample temp = agentsObservations[i].getLastSample();
			if (null != temp)
			{
				// case we get some sample until now in the loop
				if(null != lastSample)
				{
					//case the new sample is more last
					if(temp.getRound() > lastSample.getRound())
					{
						lastSample = temp;
					}
				}
				//case the first sample in the loop
				else
				{
					lastSample = temp;
				}	
			}
		}
		
		return lastSample;
	}

	/**
	 * Get number of the agent and return the observations on(about) this agent
	 * @param int - agentId
	 * @return Collection<Sample>
	 */
	public Collection<Sample> getSamplesForAgent(int agentId) {
		// case input not correct
		if(agentId < 1) {return null;}
		
		return agentsObservations[agentId - 1].getAllSamples(); 
	}

}
